﻿using System;
using System.Data.SqlClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JTLeigh.Commerce.Spencer.Core.Testing;
using JTLeigh.Commerce.Spencer.Core.Data.Exceptions;

namespace JTLeigh.Commerce.Spencer.Core.Data.UnitTests
{

	/// <summary>
	/// Unit tests for the <see cref="DatabaseExceptionTransformer"/> class.
	/// </summary>
	[TestClass]
	public class DatabaseExceptionTransformerFixture
	{

		#region Methods

		#region Transform

		/// <summary>
		/// Ensures <see cref="DatabaseExceptionTransformer.Transform"/> returns the original 
		/// exception when called with an <see cref="SqlException"/> with an unknown error 
		/// number.
		/// </summary>
		[TestMethod]
		public void TestTransformWithUnknownSqlException()
		{
			SqlException originalException;
			Exception transformedException;

			originalException = SqlExceptionCreator.Create(0);
			transformedException = DatabaseExceptionTransformer.Transform(originalException);
			Assert.AreSame(originalException, transformedException);
		}

		/// <summary>
		/// Ensures <see cref="DatabaseExceptionTransformer.Transform"/> returns a new
		/// <see cref="PrimaryKeyViolationException"/> with the original exception as its inner
		/// exception when called with an <see cref="SqlException"/> representing a primary key 
		/// violation.
		/// </summary>
		[TestMethod]
		public void TestTransformWithSqlPrimaryKeyViolationException()
		{
			SqlException originalException;
			Exception transformedException;

			originalException = SqlExceptionCreator.CreatePrimaryKeyViolation();
			transformedException = DatabaseExceptionTransformer.Transform(originalException);
			Assert.IsInstanceOfType(transformedException, typeof(PrimaryKeyViolationException));
			Assert.AreSame(originalException, transformedException.InnerException);
		}

		#endregion // Transform

		#endregion // Methods

	}

}
